import 'package:event_bus/event_bus.dart';
import 'package:in_app_purchase/in_app_purchase.dart';

class ProductManager {
  Set<String> kIds = <String>{
    "net.super.week",
    "net.super.month",
    "net.super.year"
  };

  factory ProductManager() => _getInstance();

  static ProductManager get instance => _getInstance();
  static ProductManager? _instance;
  List<ProductDetails> products = [];

  ProductManager._internal();

  static ProductManager _getInstance() {
    _instance ??= ProductManager._internal();
    return _instance!;
  }

  getProductList() async {
    final ProductDetailsResponse response =
        await InAppPurchase.instance.queryProductDetails(kIds);
    if (response.notFoundIDs.isNotEmpty) {
      // Handle the error.
    }
    products = response.productDetails;
  }

  ProductDetails getWeekProductDetail() {
    late ProductDetails targetProductDetail;
    for (ProductDetails productDetails in products) {
      if (productDetails.id == kIds.elementAt(0)) {
        targetProductDetail = productDetails;
        break;
      }
    }
    return targetProductDetail;
  }

  ProductDetails getMonthProductDetail() {
    late ProductDetails targetProductDetail;
    for (ProductDetails productDetails in products) {
      if (productDetails.id == kIds.elementAt(1)) {
        targetProductDetail = productDetails;
        break;
      }
    }
    return targetProductDetail;
  }

  ProductDetails getYearProductDetail() {
    late ProductDetails targetProductDetail;
    for (ProductDetails productDetails in products) {
      if (productDetails.id == kIds.elementAt(2)) {
        targetProductDetail = productDetails;
        break;
      }
    }
    return targetProductDetail;
  }
}
